/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.uch.syscom.hibernateSpringDao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pe.edu.uch.syscom.mapping.TbDistrito;

/**
 *
 * @author USUARIO
 */
public class HSDistritoDAOImpl extends HibernateDaoSupport implements HSDistritoDAO {

    @Override
    public List<TbDistrito> loadDepartamentos() {
        Criteria criteria;
        criteria = this.getSession().createCriteria( TbDistrito.class );
        criteria.add( Restrictions.like( "disCod", "%__0000%" ) );
        return criteria.list();
    }

    @Override
    public List<TbDistrito> loadProvincias( String sDpto ) {
        Criteria criteria;
        criteria = this.getSession().createCriteria( TbDistrito.class );
        criteria.add( Restrictions.like( "proCod", "%" + sDpto.substring( 0, 2 ) + "__%" ) );
        criteria.add( Restrictions.like( "disCod", "%" + sDpto.substring( 0, 2 ) + "__00%" ) );
        criteria.add( Restrictions.not( Restrictions.like( "disCod", "%" + sDpto.substring( 0, 2 ) + "0000%" ) ) );
        return criteria.list();
    }

    public List<TbDistrito> loadDistritos( String sProv ) {
        Criteria criteria;
        criteria = this.getSession().createCriteria( TbDistrito.class );
        criteria.add( Restrictions.like( "disCod", "%" + sProv.substring( 0, 4 ) + "__%" ) );
        criteria.add( Restrictions.not( Restrictions.like( "disCod", "%" + sProv.substring( 0, 4 ) + "00%" ) ) );
        return criteria.list();
    }

    public TbDistrito findDistrito( String sCodDistrito ) {
        return (TbDistrito) this.getSession().get( TbDistrito.class, sCodDistrito );
    }
}
